//
//  YIem_SY_Four_ViewController.m
//  B段_开眼
//
//  Created by YIem on 16/5/5.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_SY_Four_ViewController.h"
// 引入库文件 高德地图
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <WebKit/WebKit.h>



@interface YIem_SY_Four_ViewController ()<MAMapViewDelegate, AMapSearchDelegate>
{
    MAMapView *_mapView;
    AMapSearchAPI *_search;
    UILabel *_label;
}
@property (nonatomic, strong) UIWebView *web;
@end

@implementation YIem_SY_Four_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    [MAMapServices sharedServices].apiKey = @"27b59ac191c63f7bc29997ff6a0cfdc5";
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)  / 1.2)];
    _mapView.delegate = self;
//    _mapView.mapType = MAMapTypeSatellite;
    _mapView.showsUserLocation = YES;
    
    [self.view addSubview:_mapView];
    [_mapView setUserTrackingMode: MAUserTrackingModeFollowWithHeading animated:YES];
    _mapView.pausesLocationUpdatesAutomatically = NO;
    _mapView.allowsBackgroundLocationUpdates = YES;
    
    
    //
    [AMapSearchServices sharedServices].apiKey = @"27b59ac191c63f7bc29997ff6a0cfdc5";
    _search = [[AMapSearchAPI alloc]init];
    _search.delegate = self;
    


    _label = [[UILabel alloc] init];
    [self.view addSubview:_label];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mapView.mas_bottom).with.offset(0);
        make.left.right.equalTo(self.view).width.offset(0);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 70, 80));
        
    }];
    _label.numberOfLines = 0;

}
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
            regeo.location = [AMapGeoPoint locationWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
            regeo.radius = 1000;
            regeo.requireExtension = YES;
        
            [_search AMapReGoecodeSearch:regeo];
    }
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil) {
        
        NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode.formattedAddress];
        NSLog(@"地理位置 :%@", result);
        
        _label.text = [NSString stringWithFormat:@"你当前所在的位置是:\n %@" , response.regeocode.formattedAddress];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
